﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Newtonsoft.Json;
using ShkSimulation.carsharing;
using ShkSimulation.core.template.model;
using ShkSimulation.desktop.setting;
using ShkSimulation.desktop.setting.tools;

namespace ShkSimulation.desktop.component.parallel {

	public partial class ParallelConfigPanel : UserControl {

		public const string CONFIG_UNIQUE_KEY = "CONFIG_UNIQUE_KEY";
	
		private List<InputConfig> AllConfigs { get; set; }

		public IReadOnlyList<InputConfig> ParallelConfigs => AllConfigs.AsReadOnly();

		public ParallelConfigPanel() {
			InitializeComponent();
			this.Load += LoadParallelConfig;
		}

		private void LoadParallelConfig(object sender, EventArgs eventArgs) {
			string parallelConfig = ConfigManager.UserSimulationConfig.并行设置;
			if (string.IsNullOrEmpty(parallelConfig)) {
				AllConfigs = new List<InputConfig>();
				InputConfig carsharingInputConfig = ConfigManager.UserSimulationConfig.Convert();
				carsharingInputConfig.SetConfigValue(CONFIG_UNIQUE_KEY, "Default");
				AllConfigs.Add(carsharingInputConfig);
			} else {
				AllConfigs = JsonConvert.DeserializeObject<List<InputConfig>>(parallelConfig);
				if (AllConfigs == null) {
					Console.WriteLine(@"No config load!");
					AllConfigs = new List<InputConfig>();
				}
			}
			int index = 0;
			foreach (var inputConfig in AllConfigs) {
				AddTreeNode(inputConfig);
				Console.WriteLine(@"Load {0} Config: {1}", index, inputConfig.GetConfigValue<string>(CONFIG_UNIQUE_KEY));
				index++;
			}
		}
		
		private void SaveParallelConfig() {
			ConfigManager.UserSimulationConfig.并行设置 = JsonConvert.SerializeObject(AllConfigs);
		}

		private bool CheckUnique(string key) {
			foreach (var config in AllConfigs) {
				if (config.ResourcePaths[CONFIG_UNIQUE_KEY].Equals(key)) {
					return false;
				}
			}
			return true;
		}
		
		private InputConfig CreateConfig(InputConfig config = null) {
			SingleConfigForm form = new SingleConfigForm(config);
			DialogResult result = form.ShowDialog();
			if (result != DialogResult.Cancel) {
				return CheckAndConvert(form.SingleConfig);
			}
			return config;
		}

		private void AddTreeNode(InputConfig config) {
			string uniqueKey = config.GetConfigValue<string>(CONFIG_UNIQUE_KEY);
			TreeNode root = new TreeNode(uniqueKey);
			root.Nodes.Add(new TreeNode(config.GetResourceValue(CarsharingConfig.StationResourceKey)));
			root.Nodes.Add(new TreeNode(config.GetResourceValue(CarsharingConfig.RoadResourceKey)));
			root.Nodes.Add(new TreeNode(config.GetResourceValue(CarsharingConfig.DemandResourceKey)));
			ConfigTreeView.Nodes.Add(root);
		}		
		
		private InputConfig CheckAndConvert(SingleConfig config) {
			if (!CheckUnique(config.Key)) {
				MessageBox.Show(@"Key不能重复", @"提示");
				return null;
			}
			InputConfig carsharingInputConfig = ConfigManager.UserSimulationConfig.Convert();
			carsharingInputConfig.SetResourceValue(CarsharingConfig.StationResourceKey,  config.StationFile);
			carsharingInputConfig.SetResourceValue(CarsharingConfig.RoadResourceKey,  config.RoadFile);
			carsharingInputConfig.SetResourceValue(CarsharingConfig.DemandResourceKey,  config.DemandFile);
			carsharingInputConfig.SetConfigValue(CONFIG_UNIQUE_KEY, config.Key);
			return carsharingInputConfig;
		}

		private void AddConfigButton_Click(object sender, EventArgs e) {
			InputConfig carsharingInputConfig = CreateConfig();
			if (carsharingInputConfig != null) {
				AllConfigs.Add(carsharingInputConfig);
				AddTreeNode(carsharingInputConfig);
			}
			SaveParallelConfig();
		}

		private void DeleteConfigButton_Click(object sender, EventArgs e) {
			if (ConfigTreeView.SelectedNode == null) {
				return;
			}
			TreeNode curNode = ConfigTreeView.SelectedNode;
			while (curNode.Parent != null) {
				curNode = curNode.Parent;
			}
			int index = curNode.Index;
			AllConfigs.RemoveAt(index);
			ConfigTreeView.Nodes.RemoveAt(index);
			SaveParallelConfig();
		}
		
		private void EditButton_Click(object sender, EventArgs e) {
			if (ConfigTreeView.SelectedNode == null) {
				return;
			}
			TreeNode curNode = ConfigTreeView.SelectedNode;
			while (curNode.Parent != null) {
				curNode = curNode.Parent;
			}
			int index = curNode.Index;
			InputConfig origin = AllConfigs[index];
			AllConfigs.RemoveAt(index);
			ConfigTreeView.Nodes.RemoveAt(index);
			InputConfig config = CreateConfig(origin);
			AllConfigs.Add(config);
			AddTreeNode(config);
			SaveParallelConfig();
		}

		private void InfoButton_Click(object sender, EventArgs e) {
			MessageBox.Show(@"仅能配置站台、道路、需求数据，其他配置信息与主界面配置相同", @"说明");
		}

    }

}